package com.silence.whereeat.dao;

import com.silence.whereeat.model.UserInfo;
import com.silence.whereeat.service.UserInfoService;
import com.silence.whereeat.utils.DBUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户服务
 * Created by KIM on 2015/9/1.
 */
public class UserInfoDao implements UserInfoService {

    private DBUtils dbUtils;

    public UserInfoDao() {
        dbUtils = new DBUtils();
    }

    public boolean isExist(String username) {
        String sql = "SELECT COUNT(*) from userinfo WHERE user_name=?;";
        List<Object> list = new ArrayList<Object>();
        list.add(username);
        int result = -1;
        try {
            result = Integer.parseInt(dbUtils.querySingleResult(sql, list).get("COUNT(*)").toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result == 1;
    }

    public int register(UserInfo userInfo) {
        String sql = "INSERT INTO userinfo (`user_name`,`user_password`) VALUES (?,?);";
        List<Object> list = new ArrayList<Object>();
        list.add(userInfo.getUser_name());
        list.add(userInfo.getUser_password());
        int result = -1;
        try {
            result = dbUtils.updateByPreParedStatement(sql, list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public boolean login(String username, String password) {
        String sql = "SELECT COUNT(*) from userinfo WHERE user_name=? AND user_password=?;";
        List<Object> list = new ArrayList<Object>();
        list.add(username);
        list.add(password);
        int result = -1;
        try {
            result = Integer.parseInt(dbUtils.querySingleResult(sql, list).get("COUNT(*)").toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result == 1;
    }

    public int editUserInfo(UserInfo userInfo) {
        String sql = "UPDATE userinfo SET user_name=?,user_password=?,user_image=?,store_id=? WHERE user_id=?;";
        List<Object> list = new ArrayList<Object>();
        list.add(userInfo.getUser_name());
        list.add(userInfo.getUser_password());
        list.add(userInfo.getUser_image());
        list.add(userInfo.getStore_id());
        list.add(userInfo.getUser_id());
        int result = -1;
        try {
            result = dbUtils.updateByPreParedStatement(sql, list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public int deleteUserInfo(int userID) {
        String sql = "DELETE FROM userinfo WHERE user_id=?";
        List<Object> list = new ArrayList<Object>();
        list.add(userID);
        int result = -1;
        try {
            result = dbUtils.updateByPreParedStatement(sql, list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public UserInfo getUserInfo(int userID) {
        String sql = "SELECT * FROM userinfo WHERE user_id=?";
        List<Object> list = new ArrayList<Object>();
        list.add(userID);
        UserInfo userInfo = null;
        try {
            userInfo = dbUtils.querySingleResult(sql, list, UserInfo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userInfo;
    }

    public void close() {
        dbUtils.close();
    }
}
